package com.labweb.servicos;

import java.util.List;

import javax.persistence.RollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.SystemException;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.labweb.entidades.SolicitacaoSoftware;
import com.labweb.entidades.Usuario;
import com.labweb.util.HibernateUtil;

public class SolicitacaoSoftwareService {
	public SolicitacaoSoftware pesquisarPorId(Integer id) {
		Session session =  HibernateUtil.getSession();
		try {
			return (SolicitacaoSoftware) session.get(SolicitacaoSoftware.class, id);
		} finally {
			session.close();
		}
	}
	
	public void salvar(SolicitacaoSoftware solicitacaosoftware) throws Exception, SecurityException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		Session session = (Session) HibernateUtil.getSession();
		Transaction tx = (Transaction) session.beginTransaction();
		session.saveOrUpdate(solicitacaosoftware);
		tx.commit();
		session.close();
	}

	
	public List<SolicitacaoSoftware> listarTodas() {
		Session session =  HibernateUtil.getSession();
		try {
			return session.createCriteria(SolicitacaoSoftware.class).addOrder(Order.asc("data")).list();
		} finally {
			session.close();
		}
	}
	
	
	public void excluir(SolicitacaoSoftware solicitacaosoftware) throws Exception, SecurityException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SystemException {
		// TODO Auto-generated method stub
		 
			Session session = (Session) HibernateUtil.getSession();
			Transaction tx = (Transaction) session.beginTransaction();
			session.delete(solicitacaosoftware);
			tx.commit();
			session.close();

		
	}

	public List<SolicitacaoSoftware> listarTodasPendentes() {
		// TODO Auto-generated method stub
		Session session =  HibernateUtil.getSession();
		try {
			return session.createCriteria(SolicitacaoSoftware.class).add(Restrictions.eq("status", new Integer(0))).addOrder(Order.asc("data")).list();
			           
		} finally {
			session.close();
		}
	}
	
	public List<SolicitacaoSoftware> listarTodasInstalados() {
		// TODO Auto-generated method stub
		Session session =  HibernateUtil.getSession();
		try {
			return session.createCriteria(SolicitacaoSoftware.class).add(Restrictions.eq("status", new Integer(1))).addOrder(Order.asc("data")).list();
			           
		} finally {
			session.close();
		}
	}
}
